home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / comm / msged400.zip / src / helpinfo.c < prev    next >
C/C++ Source or Header  |  1996-07-24  |  3KB  |  147 lines

  1. /*
  2.  *  HELPINFO.C
  3.  *
  4.  *  Written on 10-Jul-94 by John Dennis.  Modifications by Andrew Clarke.
  5.  *  Released to the public domain.
  6.  *
  7.  *  Msged help file decompiler; displays Msged help file information and
  8.  *  contents.
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <errno.h>
  15.  
  16. #include "help.h"
  17.  
  18. static char line[255];
  19. static FILE *fp;
  20. static HFileHdr Fheader;
  21. static HTopicHdr *topics;
  22. static int setup, CurrTopic;
  23. static int numTopics;
  24.  
  25. static void intHelpInit(char *fnm)
  26. {
  27.     int i;
  28.     setup = 0;
  29.     fp = fopen(fnm, "rb");
  30.     if (fp == NULL)
  31.     {
  32.         fprintf(stderr, "HELPINFO: Error opening help source file, '%s' : %s\n",
  33.                 fnm, strerror(errno));
  34.         return;
  35.     }
  36.     fread(&Fheader, sizeof Fheader, 1, fp);
  37.     numTopics = (Fheader.topics[1] << 8) | Fheader.topics[0];
  38.     topics = calloc(numTopics, sizeof(HTopicHdr));
  39.     if (topics == NULL)
  40.     {
  41.         fprintf(stderr, "HELPINFO: Memory allocation failure!\n");
  42.         return;
  43.     }
  44.     for (i = 0; i < numTopics; i++)
  45.     {
  46.         fread(&topics[i], sizeof *topics, 1, fp);
  47.     }
  48.     setup = 1;
  49.     CurrTopic = 0;
  50. }
  51.  
  52. static void intDisplayPage(long offset)
  53. {
  54.     char *s;
  55.  
  56.     fseek(fp, offset, SEEK_SET);
  57.  
  58.     while (fgets(line, 254, fp) != NULL)
  59.     {
  60.         if (!strncmp(line, "*Page", 5) || !strncmp(line, "*End", 4))
  61.             break;
  62.  
  63.         if (*line != '\n')
  64.         {
  65.             s = strchr(line, '\n');
  66.             if (s != NULL)
  67.                 *s = '\0';
  68.  
  69.             printf("%s\n", line);
  70.         }
  71.     }
  72. }
  73.  
  74. static void intDoHelp(int topic)
  75. {
  76.     long offset[20];
  77.     int page, pages;
  78.  
  79.     if (topic < 0 || topic > numTopics)
  80.     {
  81.         return;
  82.     }
  83.     fseek(fp, topics[topic].offset, SEEK_SET);
  84.  
  85.     if (fgets(line, 254, fp) == NULL)
  86.     {
  87.         fprintf(stderr, "HELPINFO: Input line too long!\n");
  88.         return;
  89.     }
  90.  
  91.     if (strncmp(line, "*Begin", 6))
  92.         return;
  93.  
  94.     pages = 1;
  95.     offset[pages - 1] = ftell(fp);
  96.  
  97.     while (1)
  98.     {
  99.         if (fgets(line, 254, fp) == NULL)
  100.         {
  101.             fprintf(stderr, "HELPINFO: Input line too long!\n");
  102.             break;
  103.         }
  104.  
  105.         if (!strncmp(line, "*End", 4))
  106.             break;
  107.  
  108.         if (!strncmp(line, "*Page", 5))
  109.         {
  110.             pages++;
  111.             offset[pages - 1] = ftell(fp);
  112.         }
  113.     }
  114.  
  115.     fseek(fp, offset[0], SEEK_SET);
  116.  
  117.     page = 0;
  118.  
  119.     while (page < pages)
  120.     {
  121.         intDisplayPage(offset[page++]);
  122.     }
  123. }
  124.  
  125. void helpinfo(int argc, char *argv[])
  126. {
  127.     int curr;
  128.  
  129.     puts("Msged help file decompiler");
  130.  
  131.     if (argc < 2)
  132.     {
  133.         printf("\nUsage: msged -hi <helpfile_source>\n");
  134.         return;
  135.     }
  136.  
  137.     intHelpInit(argv[1]);
  138.  
  139.     curr = 0;
  140.     while (curr < numTopics)
  141.     {
  142.         intDoHelp(curr++);
  143.     }
  144.  
  145.     return;
  146. }
  147.